/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package listener;

import engine.Singleton;
import figure.Cross;
import figure.Round;
import server.ConnectClient;
import tictactoe.BeforeGame;
import tictactoe.EndGame;
import tictactoe.Offer;
import tictactoe.SmallField;
import tictactoe.UserComman;
import tictactoe.noGame;

/**
 *
 * @author Alexx
 */
public class MyRealListener implements MyListener {
    ConnectClient connectClient;
    
    public MyRealListener (ConnectClient connectClient)
  {
    this.connectClient = connectClient;
  }

    @Override
    public void addUser(MyEvent MyEvent) {
        String name = MyEvent.getName();
        Singleton.Clientusers.clear();
      while (name.length() > 1)
      {
        int end = name.indexOf("-");
        String na = "";
        if (end > 0)
        {
          na = name.substring(0, end);
          name = name.substring(end + 1);
        } else
        {
          na = name;
          name = "";
        }
        Singleton.Clientusers.add(na);
        System.out.println("hh" + na);
      }
      UserComman.addUserToList();
    }

    @Override
    public void offerUser(MyEvent MyEvent) {
        String name = MyEvent.getName();
        new Offer(name).setVisible(true);
        connectClient.setPartner(name);
    }

    @Override
    public void noGame(MyEvent MyEvent) {
        new noGame().setVisible(true);
    }

    @Override
    public void startGame(MyEvent MyEvent) {
        String figure = MyEvent.getName();
        String step = MyEvent.getOfferName();
        boolean fig = false;
        boolean fstep = false;
        if (figure.equals("Cross")) {
            fig = true;
        }
        if (step.equals("First")) {
            fstep = true;
        }
        UserComman.play(fig, fstep);
        new BeforeGame(fig, fstep).setVisible(true);
    }

    @Override
    public void partnerStep(MyEvent MyEvent) {
        String row = MyEvent.getName();
        String col = MyEvent.getOfferName();
        int r = Integer.parseInt(row);
        int c = Integer.parseInt(col);
        connectClient.steps[r][c] = true;
        SmallField now = connectClient.matrix[r][c];
        if (connectClient.fig) {
            now.add(new Round());
            now.repaint();
            connectClient.step = true;
        } else {
            now.add(new Cross());
            now.repaint();
            connectClient.step = true;
        }
        UserComman.disblock();
    }

    @Override
    public void finishGame(MyEvent MyEvent) {
        String status = MyEvent.getName();
        String end = "1";
        if (status.equals("win")) {
            end = "Победил!";
        }
        if (status.equals("lose")) {
            end = "Проиграл!";
        }
        if (status.equals("eq")) {
            end = "Ничья!";
        }

        new EndGame(end).setVisible(true);
        UserComman.clearField();
    }
    
}
